﻿using CDMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CDMS.Entities;

namespace CDMS.DAL
{
    public class Product_DAL
    {
        DBAccess db;
        public Product_DAL()
        {
            db = new DBAccess();
        }

        public DataTable GetALL()
        {
            DataTable retVal = null;
            retVal = db.GetDataTable("Product_GetALL");
            return retVal;
        }

        public DataTable GetAllByProductGroupID(int ProductGroupID)
        {
            DataTable retVal = null;
            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("ProductGroupID",ProductGroupID)
            };

            retVal = db.GetDataTable("Product_GetAllByProductGroupID", paramArray);
            return retVal;
        }

        public Product GetOne(int Product_ID)
        {

            Product retVal = null;
            DataTable dt = new DataTable();

            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("Product_ID",Product_ID)
            };

            dt = db.GetDataTable("Product_GetOne", paramArray);
            if (dt != null)
                if (dt.Rows.Count > 0)
                {
                    retVal = new Product();
                    retVal.Product_Code = dt.Rows[0]["Product_Code"].ToString();
                    retVal.Product_GuaranteePeriod = Convert.ToDecimal(dt.Rows[0]["Product_GuaranteePeriod"]);
                    retVal.Product_ID = Convert.ToInt32(dt.Rows[0]["Product_ID"]);
                    retVal.Product_ImportPrice = Convert.ToDecimal(dt.Rows[0]["Product_ImportPrice"]);
                    retVal.Product_Name = dt.Rows[0]["Product_Name"].ToString();
                    retVal.Product_ProductGroupID = Convert.ToInt32(dt.Rows[0]["Product_ProductGroupID"]);
                    retVal.Product_Remind = Convert.ToBoolean(dt.Rows[0]["Product_Remind"]);
                    retVal.Product_RetailPrice = Convert.ToDecimal(dt.Rows[0]["Product_RetailPrice"]);
                    retVal.Product_UnitID = Convert.ToInt32(dt.Rows[0]["Product_UnitID"]);
                    retVal.Product_WholesalePrice = Convert.ToDecimal(dt.Rows[0]["Product_WholesalePrice"]);
                }
            return retVal;
        }

        public int Insert(Product entity)
        {
            int retVal;
            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("Product_Code",entity.Product_Code),
                new SqlParameter("Product_GuaranteePeriod",entity.Product_GuaranteePeriod),
                new SqlParameter("Product_ImportPrice",entity.Product_ImportPrice),
                new SqlParameter("Product_Name",entity.Product_Name),
                new SqlParameter("Product_ProductGroupID",entity.Product_ProductGroupID),
                new SqlParameter("Product_Remind",entity.Product_Remind),
                new SqlParameter("Product_RetailPrice",entity.Product_RetailPrice),
                new SqlParameter("Product_UnitID",entity.Product_UnitID),
                new SqlParameter("Product_WholesalePrice",entity.Product_WholesalePrice)
            };

            retVal = Convert.ToInt32(db.ExecuteScalar("Product_Insert", paramArray));

            return retVal;
        }
        public bool Update(Product entity)
        {
            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("Product_ID",entity.Product_ID),
                new SqlParameter("Product_Code",entity.Product_Code),
                new SqlParameter("Product_GuaranteePeriod",entity.Product_GuaranteePeriod),
                new SqlParameter("Product_ImportPrice",entity.Product_ImportPrice),
                new SqlParameter("Product_Name",entity.Product_Name),
                new SqlParameter("Product_ProductGroupID",entity.Product_ProductGroupID),
                new SqlParameter("Product_Remind",entity.Product_Remind),
                new SqlParameter("Product_RetailPrice",entity.Product_RetailPrice),
                new SqlParameter("Product_UnitID",entity.Product_UnitID),
                new SqlParameter("Product_WholesalePrice",entity.Product_WholesalePrice)
            };


            return Convert.ToInt32(db.ExecuteNonQuery("Product_Update", paramArray)) > 0;
        }

        public bool Exists(Product entity)
        {
            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("Product_ID",entity.Product_ID),
                new SqlParameter("Product_Code",entity.Product_Code)
                
            };


            return Convert.ToInt32(db.ExecuteScalar("Product_Exists", paramArray)) > 0;
        }

        public bool Delete(int Product_ID)
        {
            SqlParameter[] paramArray = new SqlParameter[] 
            { 
                new SqlParameter("Product_ID",Product_ID)
            };


            return Convert.ToInt32(db.ExecuteNonQuery("Product_Delete", paramArray)) > 0;
        }
    }
}
